package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMessageDto;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.ISearchMasterMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage, SearchMasterMessageQuery> implements ISearchMasterMessageService {

    /**
     * 审核表mapper
     */
    @Autowired
    private SearchMasterMessageMapper messageMapper;
    /**
     * 接单表mapper
     */
    @Autowired
    private SearchMasterMsgMapper msgMapper;

    @Override
    public PageList<SearchMasterMessage> findPage(SearchMasterMessageQuery q) {

        //1.使用分页工具
        PageHelper.startPage(q.getPage(),q.getSize());
        //2.查询所有
        List<SearchMasterMessage> list = messageMapper.findMyPage(q);

        //4.封装pageInfo
        PageInfo<SearchMasterMessage> pageInfo = new PageInfo<>(list);
        //5.返回有数据的分页对象
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 发布寻主需求
     * @param message
     */
    @Override
    public void publish(SearchMasterMessage message) {

        //1.我们需要获取到登录人id，卖家id
        Long userId = LoginInfoUtil.getUserId();

        //2.把userId设置到message对象中
        message.setUserId(userId);

        //3.入库
        messageMapper.insertSelective(message);

    }

    @Override
    public void shenHe(SearchMasterMessageDto messageDto) {
        //1.根据id查询数据库
        SearchMasterMessage masterMessage = messageMapper.selectByPrimaryKey(messageDto.getId());

        if (masterMessage == null){
            throw new BusinessException("审核数据不存在");
        }

        Integer oldState = masterMessage.getState();
        if (oldState != 0){
            throw new BusinessException("只有待审核的数据才能操作");
        }

        //2.如果是state = 2 审核驳回
        Integer state = messageDto.getState();
        if (state == 2){
            //2.1修改状态，update数据
            masterMessage.setState(2); //驳回

            messageMapper.updateByPrimaryKeySelective(masterMessage);
        }else{
            //3.如果state =1  审核通过
            masterMessage.setState(1);
            //3.1修改状态
            messageMapper.updateByPrimaryKeySelective(masterMessage);

            //3.2copy字段
            SearchMasterMsg searchMasterMsg = BeanUtil.copyProperties(masterMessage, SearchMasterMsg.class);
            //自己看着写
            searchMasterMsg.init(messageDto.getNote());

            //3.3入库
            msgMapper.insertSelective(searchMasterMsg);

        }

    }
}
